<?php
class Artikel_model extends CI_Model
{

    //constructor 
    function artikel_model()
    {
        parent::__construct();
    }

    //function to get all article
    function getAllArtikel($limit = null, $offset = null)
    {


        $this->db->select('artikel_id,artikel_name,artikel_date');
        $this->db->order_by('artikel_id', 'desc');
        $q = $this->db->get('artikel', $limit, $offset);

        $data = array();


        if ($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
        }

        return $data;
    }

    //function to get article details
    function getDetailArtikel($id)
    {
        $this->db->select('artikel_name,artikel_date,artikel_content');
        $this->db->where('artikel_id', $id);
        $q = $this->db->get('artikel');

        $data = null;
        if ($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data = $row;
            }
        }
        
        return $data;
    }

    //untuk mendapatkan id -> masih salah fungsi ini
    function getArtikel_id()
    {

        $this->db->select('artikel_id');
        $this->db->from('artikel');
        $this->db->where('artikel_name', "Second Artikel");

        $q = $this->db->get('artikel');


        if ($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }

    /**
     * Insert new article
     * @param type $data article data
     */
    function insertArtikel($data)
    {
        $this->db->insert('artikel', $data);
        if ($this->db->affected_rows() > 0) return TRUE;
        else return FALSE;
    }

    /**
     * Update article
     * @param type $id article ID
     * @param type $data new data
     */
    function updateArtikel($id, $data){
        $this->db->update('artikel', $data, array('artikel_id' => $id));
        if($this->db->affected_rows() > 0) return true;
        else return false;
    }
    
    /**
     * Delete article
     * @param type $id article ID 
     */
    function deleteArtikel($id){
        $this->db->delete('artikel',array('artikel_id'=>$id));
        if($this->db->affected_rows() > 0) return true;
        else return false;
    }
}
?>
